package com.company.cloud.mall.modules.app.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.YNEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.mall.constant.MallErrorEnum;
import com.company.cloud.mall.modules.app.controller.req.CarDelBatchParam;
import com.company.cloud.mall.modules.app.controller.req.CartListParam;
import com.company.cloud.mall.modules.app.controller.req.CartSaveParam;
import com.company.cloud.mall.modules.app.controller.req.CartUpdateParam;
import com.company.cloud.mall.modules.app.controller.resp.CartModel;
import com.company.cloud.mall.modules.app.service.ShopCartService;
import com.company.cloud.mall.modules.base.entity.CartInfo;
import com.company.cloud.mall.modules.base.service.CartInfoService;
import com.company.cloud.product.modules.api.response.Product;
import com.company.cloud.product.modules.api.service.ProductRemoteService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 购物车
 */
@Login
@RestController
@RequestMapping("/app/mall/cart")
@Api(tags="Shop_购物车")
public class CartInfoController {
	
	@Autowired
	private CartInfoService cartInfoService;
	@Autowired
	private ProductRemoteService productRemoteService;
	@Autowired
	private ShopCartService shopCartService;
	
	@PostMapping("/query")
	@ApiOperation(value="购物车信息表列表查询", response=CartModel.class)
	public BaseResult query(@RequestBody CartListParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		List<CartModel> data=shopCartService.getUserCartList(userInfo.getId());
		return BaseResult.ok(data);
	}
	
	@PostMapping("/save")
	@ApiOperation(value="购物车新增",response =BaseResult.class )
	public BaseResult save(@RequestBody CartSaveParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		BaseClientResult<Product> prdResp=productRemoteService.getProductById(params.getProductNo());
		Product product=BaseClientResult.getResultData(prdResp);
		if(product==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		CartInfo entity=cartInfoService.getOne(new QueryWrapper<CartInfo>()
				.eq("user_id", userInfo.getId())
				.eq("product_no", params.getProductNo())
				.eq(StringUtils.hasText(params.getSkuNo()), "sku_no", params.getSkuNo())
				);
		if(entity==null) {
			entity=new CartInfo();
			entity.setCompanyNo(product.getCompanyNo());
			entity.setUserId(userInfo.getId());
			entity.setProductNo(params.getProductNo());
			entity.setProductIsUpdown(YNEnum.YES.getCode());
			entity.setSkuNo(params.getSkuNo());
			entity.setFrameNo(params.getFrameNo());
			entity.setQuantity(params.getQuantity());
			entity.addCreateParam(userInfo);
			cartInfoService.save(entity);
		}else {
			entity.setQuantity(entity.getQuantity()+params.getQuantity());
			entity.setProductIsUpdown(YNEnum.YES.getCode());
			entity.addEditParam(userInfo);
			cartInfoService.updateById(entity);
		}
		return BaseResult.ok();
	}
	
	@PostMapping("/update")
	@ApiOperation(value="购物车修改",response = BaseResult.class)
	public BaseResult update(@RequestBody CartUpdateParam params) {
		CartInfo entity=cartInfoService.getById(params.getId());
		if(null == entity) {
			throw new BusinessException(MallErrorEnum.Cart_Null);
		}
		if(params.getQuantity()<=0) {
			cartInfoService.removeById(params.getId());
		}else {
			LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
			entity.setQuantity(params.getQuantity());
			entity.addEditParam(userInfo);
			cartInfoService.updateById(entity);
		}
		return BaseResult.ok();
	}
	
	@GetMapping("/delete")
	@ApiOperation(value="购物车删除",response = BaseResult.class)
	public BaseResult delete(@RequestParam("id")Integer id) {
		CartInfo entity=cartInfoService.getById(id);
		if(null == entity) {
			throw new BusinessException(MallErrorEnum.Cart_Null);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		if(entity.getUserId().intValue()!=userInfo.getId().intValue()) {
			throw new BusinessException(BaseReturnEnum.NOT_AUTHC);
		}
		cartInfoService.removeById(id);
		return BaseResult.ok();
	}
	
	
	@PostMapping("/deleteBatch")
	@ApiOperation(value="购物车删除",response = BaseResult.class)
	public BaseResult deleteBatch(@RequestBody CarDelBatchParam params) {
		if(CollectionUtils.isEmpty(params.getIds())) {
			return BaseResult.ok();
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		cartInfoService.remove(new QueryWrapper<CartInfo>()
				.in("id", params.getIds())
				.eq("user_id", userInfo.getId())
				);
		return BaseResult.ok();
	}
	
}
